

<template>
  <div class="box">
    <hu-card style="position: relative; top: 0; left: 0;">
      <div style="font-weight: 700;">
        {{ queryData.knowledgeName }} 》{{queryData.datasetName}}
        <!-- {{ queryData.datasetType === 'QA' ? '问答' : '文本' }} -->
      </div>
      <div class="back">
        <img
          class="searh_image"
          src="@/assets/images/return.svg"
          @click="backList"
        >
      </div>
      <el-form
        :model="queryParams"
        style="margin-top: 20px;"
        label-width="0px"
      >
        <el-row :gutter="50">
          <el-col :span="6">
            <el-form-item>
              <el-input
                v-model="queryParams.keyword"
                type="text"
                @keyup.enter.native="MixinSearchList"
                placeholder="请输入关键字查询"
              >
      <el-button :loading="MixinLoading" @click="MixinSearchList" slot="append" icon="el-icon-search"></el-button>

            </el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="18">
            <el-form-item class="searchFromButton">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="MixinSearchList"
              >查询</el-button>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <el-row
        :gutter="10"
        class="mb10 space_between"
        type="flex"
      >
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
      </el-row>
      <hu-table
        :tableData="tableData"
        :loading="MixinLoading"
        :tableCol="queryData.datasetType === 'QA' ? tableCol : tableCol2"
      >
        <template v-slot:operate="{ row }">
          <el-button
            type="text"
            @click="handleUpdate(row)"
          >修改</el-button>
          <el-button
            type="text"
            @click="handleDelete(row)"
          >删除</el-button>
        </template>
      </hu-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </hu-card>

    <!-- 抽屉 添加或修改参数配置对话框 -->
    <hu-drawer
      title="片段"
      :open.sync="showModal"
      :setLook="MixinSetLook"
      :loading="MixinLoading"
      @submit="addFrom"
    >
      <!-- tent sta -->
      <el-form
        ref="queryForm"
        :model="formData"
        :rules="rulesSon"
        label-width="80px"
      >
        <template v-if="queryData.datasetType ==='QA'">
          <el-form-item
            label="问题"
            class="is-required"
            prop="question"
          >
            <el-input
              v-model="formData.question"
              placeholder="请输入问题内容"
            ></el-input>
          </el-form-item>
        </template>
        <el-form-item
          label="回答"
          prop="answer"
        >
          <el-input
            v-model="formData.answer"
            type="textarea"
            :autosize="{ minRows: 5, maxRows: 10}"
            placeholder="请输入回答内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- tent end -->
    </hu-drawer>
  </div>
</template>
<script>
import * as http from "@/api/repository/fragment";
export default {
  data() {
    return {
      // list 参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0, // table total
      // table Col
      tableCol: [
        {
          title: "序号",
          type: "index",
          width: "55",
        },
        {
          title: "问题",
          key: "question",
        },
        {
          title: "回答",
          key: "answer",
        },
        {
          title: "操作",
          slot: "operate",
          width: "200",
          fixed: "right",
        },
      ],
      tableCol2: [
        {
          title: "序号",
          type: "index",
          width: "55",
        },
        {
          title: "片段内容",
          key: "answer",
        },
        {
          title: "操作",
          slot: "operate",
          width: "200",
          fixed: "right",
        },
      ],
      tableData: [], // table Data
      showModal: false, // 弹窗
      // form Data
      formData: {
        segmentId: undefined,
      },
      // 表单校验
      rulesSon: {
        answer: [{ required: true, message: "回答不能为空", trigger: "blur" }],
      },
      queryData: {},
    };
  },
  created() {
    this.queryData = this.$route.query;

    this.getList();
  },
  methods: {
    backList() {
      // const routes = this.$router.currentRoute.matched;
      // const prevRoute = routes[routes.length - 2]; // 上一个路由
      // if (prevRoute) {
      //   console.log('来自页面:', prevRoute);
      // } else {
      //   console.log('是从首页或者外部链接进入的');
      // }
      // this.$router.go(-1);

      this.$router.push({
        path: `/index/details`,
        query: {
          ...this.queryData,
        },
      });
    },
    // 列表 List
    getList() {
      this.MixinLoading = true;
      let params = {
        ...this.queryParams,
        datasetId: this.queryData.datasetId,
        knowledgeId: this.queryData.knowledgeId,
      };
      http.getList(params).then(({ stateCode, result }) => {
        if (stateCode == 200) {
          this.MixinLoading = false;
          this.tableData = result.records;
          this.total = result.total;
        }
      });
    },
    // 表单重置
    reset() {
      this.MixinSetLook = "";
      this.formData = {};
      this.resetForm("queryForm");
    },
    // add 弹出
    handleAdd() {
      this.reset();
      this.MixinSetLook = 1;
      this.showModal = true;
    },
    // Update 弹出
    handleUpdate(row) {
      this.reset();
      this.MixinSetLook = 2;
      this.formData = {
        ...row,
      };
      this.showModal = true;
    },
    // 提交
    addFrom() {
      this.$refs["queryForm"].validate((valid) => {
        if (valid) {
          this.MixinLoading = true;
          let params = {
            ...this.formData,
            datasetId: this.queryData.datasetId,
            knowledgeId: this.queryData.knowledgeId,
          };
          if (this.formData.segmentId != undefined) {
            http
              .updateList(params)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.showModal = false;
                this.getList();
              })
              .finally(() => {
                this.MixinLoading = false;
              });
          } else {
            http
              .addList(params)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.showModal = false;
                this.MixinSearchList();
              })
              .finally(() => {
                this.MixinLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除数据项？")
        .then(() => {
          return http.deleteList(row.segmentId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
    },
  },
};
</script>

<style lang='scss' scoped>
.back {
  position: absolute;
  right: 0;
  top: 0;
  height: 25px;
  border-top: 0;
  border-right: 0;
  background-color: #dddddd;
  border-radius: 0px 8px 0px 8px;
  padding: 0 10px;
  .searh_image {
    width: 22px;
    transition: 0.1s;
    cursor: pointer;
    &:hover {
      transform: scale(1.02);
    }
  }
}
</style>
